package yuan.deng.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
import yuan.deng.domain.Person;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */

@RestController
@RequestMapping("/personC")
public class PersonController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/findById/{id}")
    public Person findById(@PathVariable("id")int id){

        System.out.println("消费者启动成功");

        String url1="http://localhost:8000/personP/findById/"+id;
        Person person1 = restTemplate.getForObject(url1, Person.class);
        System.out.println(person1);

        List<ServiceInstance> instances = discoveryClient.getInstances("EUREKA-PROVIDER");
        //List<ServiceInstance> instances1 = discoveryClient.getInstances("EUREKA-PROVIDER");

        if (instances==null || instances.size()==0){
            System.out.println("111");
            return null;
        }
        ServiceInstance instance = instances.get(0);

        String host = instance.getHost();
        int port = instance.getPort();
        System.out.println(host);
        System.out.println(port);
        String url="http://"+host+":"+port+"/personP/findById/"+id;

        Person person=restTemplate.getForObject(url,Person.class);
        System.out.println("222");
        return person;
       //return null;
    }
    /*@GetMapping("/findById/{id}")
    public Person findById(@PathVariable("id")int id){
        String url="http://EUREKA-PROVIDER/personP/findById/"+id;
        Person person=restTemplate.getForObject(url,Person.class);
        System.out.println("222");
        return person;
    }*/

     /*@GetMapping("/findById/{id}")
    public Person findById(@PathVariable("id")int id){
        String url="http://EUREKA-PROVIDER/personP/findById/"+id;
        Person person=restTemplate.getForObject(url,Person.class);
        System.out.println("222");
        return person;
    }*/
}
